﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml;
using System.IO;

namespace FeedNet
{
    public class Feed
    {
        private string _title;
        private string _url;
        private string _feed_url;
        private string _etag;
        private DateTime _last_modified;
        private string _description;

        private List<Entry> _entries;
        private List<Entry> _new_entries;

        public string title
        {
            get { return _title; }
            set { _title = value; }
        }

        public string url
        {
            get { return _url; }
            set { _url = value; }
        }

        public string feed_url
        {
            get { return _feed_url; }
            set { _feed_url = value; }
        }

        public string etag
        {
            get { return _etag; }
            set { _etag = value; }
        }

        public DateTime last_modified
        {
            get { return _last_modified; }
            set { _last_modified = value; }
        }

        public string description
        {
            get { return _description; }
            set { _description = value; }
        }

        public List<Entry> entries
        {
            get { return _entries; }
            set { _entries = value; }
        }

        public List<Entry> new_entries
        {
            get { return _new_entries; }
            set { _new_entries = value; }
        }

        /*
         * 
         *  Load This Feed Object from an IO stream
         * 
         */
        public void loadFromStream(Stream s)
        {
            XmlTextReader reader = new XmlTextReader(s);
            reader.Read();
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                        case "title":
                            title = reader.ReadString();
                            break;
                        case "subtitle":
                        case "description":
                            description = reader.ReadString();
                            break;
                        case "link":
                            if (reader.AttributeCount > 0 && url == null)
                            {
                                url = reader.GetAttribute("href");
                                int q = 0;
                            }
                            else if (url == null)
                            {
                                url = reader.ReadString();
                            }
                            break;
                        case "updated":
                        case "pubDate":
                            last_modified = DateTime.Parse(reader.ReadString());
                            break;
                    }

                    if (reader.Name == "item" || reader.Name == "entry")
                    {
                        entries.Add(Entry.readEntry(reader));
                    }


                }
            }
        }

        

        public Feed()
        {
            entries = new List<Entry>();
            new_entries = new List<Entry>();
        }
    }
}
